package com.rongc.client.freight.business.waybill.view.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.LogUtils;
import com.rongc.client.core.utils.StringUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.UniApplication;
import com.rongc.client.freight.base.BaseFragment;
import com.rongc.client.freight.base.model.BaseDic;
import com.rongc.client.freight.base.view.adapter.RecyclerBaseAdapter;
import com.rongc.client.freight.base.view.widget.DividerItemDecoration;
import com.rongc.client.freight.base.view.widget.EmptyRecyclerView;
import com.rongc.client.freight.base.view.widget.PullToRefreshEmptyRecyclerView;
import com.rongc.client.freight.business.carrier.model.ConveyanceDic;
import com.rongc.client.freight.business.supply.model.SupplyDic;
import com.rongc.client.freight.business.waybill.model.WayBillBean;
import com.rongc.client.freight.business.waybill.model.WayBillList;
import com.rongc.client.freight.business.waybill.request.api.HostWayBillApi;
import com.rongc.client.freight.business.waybill.request.api.HostWayBillSJApi;
import com.rongc.client.freight.business.waybill.view.adapter.RecyclerWaybillListAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by ccqx on 2017/8/9.
 */

public class WaybillFragment extends BaseFragment {
    @Bind(R.id.recycler)
    PullToRefreshEmptyRecyclerView mRecyclerView;
    RecyclerWaybillListAdapter mLvAdapter;

    List<WayBillBean> lsNews;
    int page = 1;
    String status;

    public static WaybillFragment getInstance(String status){
        WaybillFragment fragment = new WaybillFragment();
        Bundle bdl = new Bundle();
        fragment.setArguments(bdl);
        fragment.status = status;
        return fragment;
    }

    Response.Listener<WayBillList> respNewsListener = new Response.Listener<WayBillList>() {

        @Override
        public void onResponse(WayBillList response) {
            if (HandleCode.requestSuccess()) {
                if(response != null && response.getList() != null){
                    if(page == 1)
                        lsNews.clear();
                    lsNews.addAll(response.getList());
                    mLvAdapter.notifyDataSetChanged();
                }
            }
            mRecyclerView.onRefreshComplete();
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            mRecyclerView.onRefreshComplete();
            LogUtils.i("error.getMessage(): " + error.getMessage());
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.universal_recycler_pull;
    }

    @Override
    public void initView() {
        lsNews = new ArrayList<>();

        mLvAdapter = new RecyclerWaybillListAdapter(getActivity(),lsNews);
        mRecyclerView.getRefreshableView().setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
        mRecyclerView.getRefreshableView().addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.getRefreshableView().setAdapter(mLvAdapter);

        mLvAdapter.setOnItemClickListener(new RecyclerBaseAdapter.OnItemClickListener<WayBillBean>() {
            @Override
            public void onItemClick(View view, int position, WayBillBean data) {
                switch (UniApplication.getInstance().getType()) {
                    case BaseDic.IdentityType.DRIVING_TYPE:
                        ConveyanceDic.jumpActivity(getActivity(), StringUtils.parseInt(data.getWaystate()),data);
                        break;
                    case BaseDic.IdentityType.OWNER_TYPE:
                        SupplyDic.jumpActivity(getActivity(),StringUtils.parseInt(data.getWaystate()),data);
                        break;
                }
            }
        });
    }

    @Override
    public void initData() {
        switch (UniApplication.getInstance().getType()){
            case BaseDic.IdentityType.DRIVING_TYPE:
                RequestManager.getInstance().call(new HostWayBillSJApi(new HostWayBillSJApi.HostWayBillSJParams(
                        UniApplication.getInstance().getUserInfo().getUserId(),page+"",status),
                        respNewsListener, errorListener));
                break;
            case BaseDic.IdentityType.OWNER_TYPE:
                RequestManager.getInstance().call(new HostWayBillApi(new HostWayBillApi.HostWayBillParams(
                        UniApplication.getInstance().getUserInfo().getUserId(),page+"",status),
                        respNewsListener, errorListener));
                break;
        }
    }

    @Override
    public void initListener() {
        mRecyclerView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<EmptyRecyclerView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<EmptyRecyclerView> refreshView) {
                page = 1;
                initData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<EmptyRecyclerView> refreshView) {
                page++;
                initData();
            }
        });
    }
}
